package com.humanmonth.home.component.page.htree
{
	import com.humanmonth.home.component.page.htree.layout.Layout;
	import com.humanmonth.util.arrow.ArrowStyle;
	import com.humanmonth.util.arrow.GraphicsUtil;
	import com.humanmonth.util.effect.PassbyEffectGrow;
	
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	/**
	 * 连线
	 * @author 林子龙
	 * 
	 */
	public class MyLine extends UIComponent
	{
		/**
		 *所有线 
		 */
		public static var allLines:ArrayCollection=new ArrayCollection();
		/**
		 *开始节点 
		 */
		public var startNode:NodeAs;
		/**
		 *结束节点 
		 */
		public var endNode:NodeAs;
		/**
		 *连线开始点 
		 */
		public var startPoint:Point;
		/**
		 *连线结束点 
		 */
		public var endPoint:Point;

		/**
		 *箭头样式 
		 */
		private static var arrowStyle:ArrowStyle=null;
		
		public function MyLine(startNodeId:Number,endNodeId:Number)
		{
			super();
			this.startNode=NodeAs.getNodeById(startNodeId);
			this.endNode=NodeAs.getNodeById(endNodeId);
			allLines.addItem(this);
			this.initArrowStyle();
			if(Application.application.hTree.whiteBoard!=null){
			Application.application.hTree.whiteBoard.addChild(this);
			}
			drawAll();
			new PassbyEffectGrow().applyFilter(this);
			this.addEventListener(MouseEvent.DOUBLE_CLICK,popUpEditLine);
			this.doubleClickEnabled=true;
			this.addEventListener(FlexEvent.CREATION_COMPLETE,initMenu);
			this.toolTip="右击可以删除或编辑连线";
			this.graphics.lineStyle(0.5, 0x9ea4a7);
			this.graphics.beginFill(0x9ea4a7);
		}
		
		/**
		 *初始化箭头样式 
		 */
		private function initArrowStyle():void{
			if(arrowStyle==null){
				arrowStyle = new ArrowStyle();
				arrowStyle.headWidth = 8;
				arrowStyle.headLength = 10;
				arrowStyle.shaftThickness = 1;
				arrowStyle.shaftPosition = 0.2;
				arrowStyle.edgeControlPosition = 0;
				arrowStyle.edgeControlSize = 1;		
			}
		}
		
		/**
		 * 弹出编辑窗口
		 */
		private function popUpEditLine(event:Event):void{
			var lineForm:MyLineFormAs=MyLineFormAs(PopUpManager.createPopUp(Application.application.hTree.whiteBoard,MyLineFormAs,true));
			PopUpManager.centerPopUp(lineForm);
			lineForm.setLine(this);
		}
		
		/**
		 *初始化菜单 
		 */
		private function initMenu(event:Event):void{
			var deleteLineItem : ContextMenuItem = new ContextMenuItem("删除当前连线");
			deleteLineItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, deleteLine);
			var editLineItem : ContextMenuItem = new ContextMenuItem("编辑当前连线");
			editLineItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, popUpEditLine);
			var contextMenu:ContextMenu=new ContextMenu();
			contextMenu.hideBuiltInItems();
			contextMenu.customItems.push(deleteLineItem,editLineItem);
			this.contextMenu=contextMenu;
		}
		
		/**
		 * 删除线段 
		 */
		private function deleteLine(event:Event):void{
			MyLine.allLines.setItemAt(this,0);
			MyLine.allLines.removeItemAt(0);
			Application.application.hTree.whiteBoard.removeChild(this);
			MyLine.drawAll();
		}
		
		/**
		 * 找点间的距离
		 * @param start_开始数
		 * @param end_结束数
		 * @return 距离
		 */
/* 		private function getDistance(start:int,end:int):Number{
			var distance:Number=100000;
			for (var i:int=start;i< end;i++){
				for (var j:int=start;j<end;j++){
					var tD:Number=Point.distance(startNode.points[i],endNode.points[j]);
					if(tD<distance){
						distance=tD;
					}
				}
			}
			return distance;		
		} */
		
		/**初始化起始和结束节点
		 * @param start_开始数
		 * @param end_结束数
		 */
/* 		private function initStartAndEndPoints(start:int,end:int):void{
			var distance:Number=100000;
			for (var i:int=start;i< end;i++){
				for (var j:int=start;j<end;j++){
					var tD:Number=Point.distance(startNode.points[i],endNode.points[j]);
					if(tD<distance){
						distance=tD;
						this.startPoint=startNode.points[i];
						this.endPoint=endNode.points[j];
					}
				}
			}		
		} */
		
		/**
		 * 初始化连线点 
		 */
/* 		private function initPoints():void{
			var hDistance:Number=this.getDistance(2,4);
			var vDistance:Number=this.getDistance(0,2);
			if(Layout.isVertical){
				if(hDistance<vDistance-80){
					this.initStartAndEndPoints(2,4);
				}else{
					this.initStartAndEndPoints(0,2);
				}
			}else{
				if(hDistance<vDistance+80){
					this.initStartAndEndPoints(2,4);
				}else{
					this.initStartAndEndPoints(0,2);
				}			
			}
		}
	 */	
		private function initPoints2():void{
			if(Layout.isVertical){
				if(Math.abs(this.startNode.y-this.endNode.y)<this.startNode.height){
					if(this.startNode.x<this.endNode.x){
						this.startPoint=this.startNode.points[3];
						this.endPoint=this.endNode.points[2];
					}else{
						this.startPoint=this.startNode.points[2];
						this.endPoint=this.endNode.points[3]
					}
				}else{
					if(this.startNode.y<this.endNode.y){
						this.startPoint=this.startNode.points[1];
						this.endPoint=this.endNode.points[0];
					}else{
						this.startPoint=this.startNode.points[0];
						this.endPoint=this.endNode.points[1];
					}
				}
			}else{
				if(Math.abs(this.startNode.x-this.endNode.x)<this.startNode.height){
					if(this.startNode.y<this.endNode.y){
						this.startPoint=this.startNode.points[1];
						this.endPoint=this.endNode.points[0];
					}else{
						this.startPoint=this.startNode.points[0];
						this.endPoint=this.endNode.points[1]
					}
				}else{
					if(this.startNode.x<this.endNode.x){
						this.startPoint=this.startNode.points[3];
						this.endPoint=this.endNode.points[2];
					}else{
						this.startPoint=this.startNode.points[2];
						this.endPoint=this.endNode.points[3];
					}
				}			
			}
		}
		
		/**
		 *绘线 
		 */
		public function drawOne():void{
		//	this.initPoints();
			this.initPoints2();
			this.graphics.clear();
			this.graphics.beginFill(0x9ea4a7);
			GraphicsUtil.drawArrow(this.graphics, startPoint,endPoint, arrowStyle);
		}
		
		/**
		 * 绘制相关线段
		 */
		public static function drawRelate(node:NodeAs):void{
			for each(var line:MyLine in allLines){
				if(line.startNode==node||line.endNode==node){
					line.drawOne();
				}
			}
		}
		
		/**
		 *绘制所有的线条 
		 * 
		 */
		public static function drawAll(obj:Object=null):void{
			for each(var line:MyLine in allLines){
				line.drawOne();
			}
		}
		
	}
}